package com.roadmap.util.chart;

import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickUnit;
import org.jfree.chart.axis.DateTickUnitType;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.general.Dataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.Quarter;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.TextAnchor;

import com.roadmap.common.BaseConst;
import com.roadmap.common.RdcLogger;
import com.roadmap.common.RdcResourceBundle;

public class RdcDataSeriesChart extends DataChart {
	// private Date _minDate = null;
	// private Date _maxDate = null;
	//private DateTickUnit _tickUnit = null;
	// private final static int AXIS_X_COUNT = BaseConst.CHART_AXIS_X_POINTS; //
	// points of axis-X

	private final static Logger logger = RdcLogger
			.getLogger(RdcDataSeriesChart.class);

	public RdcDataSeriesChart() {
		try {
			// int diff = BaseUtility.getTimeDiffBetween(max, min, TimeType.DAY)
			// / AXIS_X_COUNT;
			// if(diff == 0){
			// diff = 1;
			// }
			// _minDate = min;
			// _maxDate = max;
			//_tickUnit = new DateTickUnit(DateTickUnitType.DAY, BaseConst.CHART_AXIS_X_POINTS,
			//		BaseConst.DATE_FORMATE);
			_chart = ChartFactory.createTimeSeriesChart("", "", "", null, true,
					true, false);
			initChart(_chart);
		}
		catch (Exception e) {

		}
	}

	public synchronized String convertDataSetToPNG(String title,
			Dataset dataset, int width, int height, HttpSession session)
			throws IOException {
		if (title != null) {
			_title.setText(title);
		}
		if (session == null) {
			return null;
		}

		((XYPlot) _chart.getPlot()).setDataset((XYDataset) dataset);
		String fname = ServletUtilities.saveChartAsPNG(_chart, width, height,
				session);

		if (logger.isDebugEnabled()) {
			logger.debug("saving chart to PNG completed for " + fname);
		}

		return fname;
	}

	protected void initPlot(Plot plot) {
		XYPlot xyplot = (XYPlot) plot;
		// xyplot.addRangeMarker(new ValueMarker(550D));
		// Quarter quarter = new Quarter(2, 2002);
		// xyplot.addDomainMarker(new
		// ValueMarker(quarter.getMiddleMillisecond()));
		xyplot.setBackgroundPaint(_background);
		xyplot.setNoDataMessage(RdcResourceBundle.CHART_NO_DATA_MESSAGE);
		xyplot.setOutlineVisible(false);

		// horizontal axis
		DateAxis dateAxis = (DateAxis) xyplot.getDomainAxis(); // xyplot.getDomainAxis();
		dateAxis.setTickLabelFont(_labelFont);
		dateAxis.setTickLabelPaint(_fontColor);
		dateAxis.setVerticalTickLabels(true);
		//dateAxis.setTickUnit(_tickUnit);
		dateAxis.setMinorTickCount(BaseConst.CHART_AXIS_X_POINTS);
		// dateAxis.setMinimumDate(_minDate);
		// dateAxis.setMaximumDate(_maxDate);

		// vertical axis
		ValueAxis valueAxis = xyplot.getRangeAxis();
		valueAxis.setTickLabelFont(_labelFont);
		valueAxis.setTickLabelPaint(_fontColor);

		// XYItemRenderer xyitem = xyplot.getRenderer();
		// xyitem.setBaseItemLabelsVisible(true);
		// xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(
		// ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
		// xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
		// xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));
		// xyplot.setRenderer(xyitem);
	}

	public Dataset getTestData1() {
		TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
		TimeSeries timeseries = new TimeSeries("实际进度");
		timeseries.add(new Quarter(1, 2001), 500.19999999999999D);
		timeseries.add(new Quarter(2, 2001), 694.10000000000002D);
		timeseries.add(new Quarter(3, 2001), 734.39999999999998D);
		timeseries.add(new Quarter(4, 2001), 453.19999999999999D);
		timeseries.add(new Quarter(1, 2002), 500.19999999999999D);
		timeseries.add(new Quarter(2, 2002), 694.10000000000002D);
		timeseries.add(new Quarter(3, 2002), 734.39999999999998D);
		timeseries.add(new Quarter(4, 2002), 453.19999999999999D);
		timeseriescollection.addSeries(timeseries);

		timeseries = new TimeSeries("预定进度");
		timeseries.add(new Quarter(1, 2001), 300.19999999999999D);
		timeseries.add(new Quarter(2, 2001), 494.10000000000002D);
		timeseries.add(new Quarter(3, 2001), 634.39999999999998D);
		timeseries.add(new Quarter(4, 2001), 253.19999999999999D);
		timeseries.add(new Quarter(1, 2002), 600.19999999999999D);
		timeseries.add(new Quarter(2, 2002), 394.10000000000002D);
		timeseries.add(new Quarter(3, 2002), 534.39999999999998D);
		timeseries.add(new Quarter(4, 2002), 653.19999999999999D);
		timeseriescollection.addSeries(timeseries);
		return timeseriescollection;
	}

	public Dataset getTestData() {
		TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
		TimeSeries timeseries = new TimeSeries("实际进度");
		timeseries.add(new Day(10, 1, 2001), 500.19999999999999D);
		timeseries.add(new Day(19, 2, 2001), 694.10000000000002D);
		timeseriescollection.addSeries(timeseries);
		
		timeseries = new TimeSeries("实际进度");
		timeseries.add(new Day(17, 1, 2001), 600.19999999999999D);
		timeseries.add(new Day(9, 2, 2001), 504.10000000000002D);
		timeseries.add(new Day(20, 3, 2001), 304.39999999999998D);
		timeseries.add(new Day(7, 4, 2001), 453.19999999999999D);
		timeseries.add(new Day(15, 5, 2001), 600.19999999999999D);
		timeseries.add(new Day(14, 6, 2001), 454.10000000000002D);
		timeseries.add(new Day(17, 7, 2001), 624.39999999999998D);
		timeseries.add(new Day(18, 8, 2001), 503.19999999999999D);
		timeseriescollection.addSeries(timeseries);

		return timeseriescollection;
	}

}
